package com.ieasy.module.oa.project.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.util.NumberUtil;
import com.ieasy.basic.util.date.DateUtils;
import com.ieasy.basic.util.dbutil.IDBUtilsHelper;
import com.ieasy.module.oa.project.entity.PosistionQuotientReportEntity;
import com.ieasy.module.oa.project.entity.ProjectCenter;
import com.ieasy.module.oa.project.web.form.CalQuotientForm;
import com.ieasy.module.oa.project.web.form.CompanyTRRYReportForm;
import com.ieasy.module.oa.project.web.form.PosistionQuotientReportForm;
import com.ieasy.module.oa.project.web.form.ProjectCenterForm;
import com.ieasy.module.system.entity.PersonEntity;

@Service
@Transactional
public class CompanyTRRYReportService implements ICompanyTRRYReportService {
	
	@Autowired
	private IDBUtilsHelper dbutil;
	
	@Autowired
	private IBaseDao<PosistionQuotientReportEntity> basedaoPositionQuotient;
	
	@Autowired
	private IBaseDao<PersonEntity> basedaoPerson ;
	
	@Autowired
	private IBaseDao<ProjectCenter> basedaoProject;
	
	@Autowired
	private IProjectDevWorkTimeService projectDevWorkTimeService ;
	
	@Autowired
	private ICompanyProjectQuotientService companyProjectQuotientService ;
	
	@Autowired
	private IProjectRyListService ryListService ;
	
	@Autowired
	private IPositionQuotienService positionQuotienService ;

	@SuppressWarnings("unchecked")
	@Override
	public DataGrid datagrid(CompanyTRRYReportForm form) {
		List<CalQuotientForm> list = new ArrayList<CalQuotientForm>() ;
		
		Map<String, Object> map = new LinkedHashMap<String, Object>() ;
		map.put("1月", new CalQuotientForm("1月"));
		map.put("2月", new CalQuotientForm("2月"));
		map.put("3月", new CalQuotientForm("3月"));
		map.put("4月", new CalQuotientForm("4月"));
		map.put("5月", new CalQuotientForm("5月"));
		map.put("6月", new CalQuotientForm("6月"));
		map.put("7月", new CalQuotientForm("7月"));
		map.put("8月", new CalQuotientForm("8月"));
		map.put("9月", new CalQuotientForm("9月"));
		map.put("10月", new CalQuotientForm("10月"));
		map.put("11月", new CalQuotientForm("11月"));
		map.put("12月", new CalQuotientForm("12月"));
		
		//1. 取得部门的所有项目（筛选条件：年份、绩效项目=是、预定最大投入人月数>0）
		for(String deptId : form.getDeptIds()) {
			List<ProjectCenterForm> project = getProject(form.getYear(), deptId);
			for (ProjectCenterForm p : project) {
				//System.out.println(p.getProj_name() + "=="+ "是否绩效：" + p.getProj_performance() + "==" + "最大预定人月数：" + p.getProj_bjzry() + "==" + p.getProj_start_time() + "==" + p.getProj_end_time());
				//绩效为是的项目才计算
				if(null != p.getProj_performance() && "是".equals(p.getProj_performance())) {
					//实际受注人月数>0的才计算
					if(null != p.getProj_bjzry() && Float.parseFloat(p.getProj_bjzry()) > 0) {
						PosistionQuotientReportForm quotientReportForm = new PosistionQuotientReportForm() ;
						quotientReportForm.setProjectId(p.getId());
						quotientReportForm.setYear(form.getYear());
						quotientReportForm.setStartYear(DateUtils.getYear(p.getProj_start_time()));
						quotientReportForm.setEndYear(DateUtils.getYear(p.getProj_end_time()));
						
						DataGrid calByQuotient = this.positionQuotienService.calByQuotient(quotientReportForm) ;
						List<CalQuotientForm> rows = (List<CalQuotientForm>) calByQuotient.getRows();
						
						
						for (CalQuotientForm row : rows) {
							
							CalQuotientForm mapRow = (CalQuotientForm) map.get(row.getMonth()) ;
							if(mapRow != null) {
								//只有最大预定投入人月数>0的才进行累加
								if(row.getFirst() > 0 && row.getRyTotal() > 0) {
									mapRow = new CalQuotientForm(
											row.getMonth(), 
											NumberUtil.formatNum(mapRow.getFirst()+row.getFirst()), 
											NumberUtil.formatNum(mapRow.getRyTotal()+row.getRyTotal()), 
											NumberUtil.formatNum(mapRow.getResult()+row.getResult())
									);
									map.put(row.getMonth(), mapRow) ;
								}
							} else {
								//只有最大预定投入人月数>0的才进行累加
								if(row.getFirst() > 0 && row.getRyTotal() > 0) {
									map.put(row.getMonth(), row) ;
								}
							}
							//System.out.println(JSON.toJSON(row));
						}
						
						System.out.println("===================================================");
						
					}
				}
			}
		}
		
		//Collections.sort(CalQuotientForm);
		//呈现数据
		for (String key : map.keySet()) {
			list.add((CalQuotientForm)map.get(key));
		}
		//月份不够补齐，补齐12个月
		/*if(map.size() < 12) {
			for(int i=map.size()+1; i<=12; i++) {
				//list.add(new CalQuotientForm(i+"月"));
			}
		}*/
		
		DataGrid dg = new DataGrid();
		dg.setRows(list);
		
		return dg;
	}
	
	private List<ProjectCenterForm> getProject(Integer year, String deptId) {
		String sql = "select " + 
				"p.id, p.proj_num, p.proj_name, p.proj_start_time, p.proj_end_time, p.proj_owner_dept_id, p.proj_bjzry, p.proj_performance " + 
				"from ieasy_oa_project_center p " +
				"where 1=1";
		
		Map<String, Object> alias = new HashMap<String, Object>();
		//sql += " and p.proj_performance=:proj_performance ";
		//alias.put("proj_performance", "是");
		
		if (deptId != null) {
			sql += " and p.proj_owner_dept_id=:deptId ";
			alias.put("deptId", deptId);
		}
		/*if (year != null) {
			sql += " and date_format(p.proj_end_time,'%Y') >= :year ";
			alias.put("year", year);
		}*/
		return this.basedaoPerson.listSQL(sql, alias, ProjectCenterForm.class, false) ;
	}

}
